package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import cn.flying.system.domain.cm.CmSeq;
import cn.flying.system.domain.cm.CmSeqExample;
import cn.flying.system.mapper.cm.CmSeqMapper;
import cn.flying.system.service.cm.CmSeqService;

/**
 * @description:
 * @author: lvyong
 * @date: 2022年05月22日 08:32
 * @version: 1.0
 */
@Service
public class CmSeqServiceImpl implements CmSeqService {

    @Resource
    private CmSeqMapper cmSeqMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String getSeq(String type) {
        CmSeqExample example = new CmSeqExample();
        CmSeqExample.Criteria criteria = example.createCriteria();
        criteria.andSeqTypeEqualTo(type);
        List<CmSeq> list = cmSeqMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        CmSeq cmSeq = list.get(0);
        Long id;
        if (StringUtils.isEmpty(cmSeq.getCurSeqNo())) {
            id = cmSeq.getMinValue();
        } else {
            id = cmSeq.getCurSeqNo() + 1;
        }
        cmSeq.setCurSeqNo(id);
        cmSeqMapper.updateByPrimaryKeySelective(cmSeq);
        return String.valueOf(id);
    }
}
